﻿# -*- coding:utf-8 -*-
#导入and方法，实现逻辑与查询
from sqlalchemy import and_
from flask import Blueprint, render_template, redirect, url_for
from gomoku.exts import mail
from flask_mail import Message
from flask import request
from flask import jsonify
from gomoku.blueprints.forms import RegisterForm
import gomoku.sqlite

import gomoku.baiduspeechreckit.recordUtils
# 引入加密算法
from werkzeug.security import generate_password_hash, check_password_hash
# 引入数字编码
import string
# 引入随机函数
import random
# /user

bp = Blueprint("user", __name__, url_prefix="/user")

# 登录路由
@bp.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template("login.html")
    else:
        data =  request.form.to_dict()
        tempUsername = data.get('username')
        tempPassword = data.get('password')
        if gomoku.sqlite.select_user(tempUsername) is None:

            return "用户名或密码错误"

        else:
            if check_password_hash(gomoku.sqlite.select_user(tempUsername)[0], tempPassword):

                return redirect("/user/game")
            else:
                return "用户名或密码错误"

# 注册路由
@bp.route("/register", methods=['GET', 'POST'])
def register():
    # 如果是get请求，返回渲染好的模板
    if request.method == 'GET':
        return render_template("register.html")
    #  如果是post请求，返回处理的结果
    else:
        # 获取form
        form = RegisterForm(request.form)
        # 验证表单 
        if form.validate():
             username = form.username.data
             password = form.password.data
             gomoku.sqlite.insert_user(username, generate_password_hash(password))
             return redirect(url_for("user.login"))
        else:
             return redirect(url_for("user.register"))


# 邮箱测试
@bp.route("/maiLtest")
def test_mail():
    message = Message(subject="zc12138", recipients=["1172954719@qq.com"], body="zc12138")
    mail.send(message)
    return "success"

# 邮箱验证码
@bp.route("/mail", methods=['GET'])
def get_code():
    # 获取目标邮箱
    email = request.args.get("email")
    # 产生随机验证码
    source = string.digits*6
    # 随机验证码采样
    code = random.sample(source, 6)
    # 将数字连成一串
    code = "".join(code)
    message = Message(subject="code", recipients=[email], body=code)
    mail.send(message)
    # 没有redis把验证码的验证交给前端
    return jsonify({"code": 200, "message": "success", "data": code})

# 五子棋界面
@bp.route("/game", methods = [ 'GET', 'POST'])
def game():

    if request.method == 'GET':
        return render_template("game.html")
    else:
        return gomoku.baiduspeechreckit.recordUtils.recordAndReconition()

@bp.route("/record", methods = ['POST'])
def record():
    data = request.form.to_dict()
    gomoku.sqlite.insert_chess(data.get('chess_num'), data.get('username'), data.get('chess_index'), data.get('step'), data.get('color'))
    return "OK"

@bp.route("/all_chess", methods = ['GET'])
def all_chess():
    result = gomoku.sqlite.get_chess()
    print(result)
    return result

@bp.route("/chess", methods = ['GET'])
def chess():
    data = request.form.to_dict()
    result = gomoku.sqlite.select_user(data.get('chess_num'))
    return result